package com.greensuisse.ready.image.resize;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.imgscalr.Scalr;

import com.greensuisse.console.Consolse;

/**
 * Hello world!
 * 
 */
public class Resize {
	private static final String OUTPUT_FORMAT = "jpg";

	public static void main(String[] args) throws IOException {
		String sourcePath, destPath;
		int size;

		System.out.println();
		sourcePath = Consolse.readLine("Source:", "D:\\temp\\in");
		destPath = Consolse.readLine("Dest:", "D:\\temp\\out");
		size = Integer.parseInt(Consolse.readLine("Max size:", "400"));

		File folder = new File(sourcePath);
		File[] files = folder.listFiles();

		for (File file : files) {
			String dest = destPath + "\\" + file.getName() + ".out."
					+ OUTPUT_FORMAT;
			File outputfile = new File(dest);
			convert(file, outputfile, size);
			file.deleteOnExit();
		}
	}

	private static void convert(File source, File dest, int size)
			throws IOException {

		System.out.println("Read " + source.getAbsolutePath());
		BufferedImage image = ImageIO.read(source);
		System.out.println("Scale ...");
		BufferedImage out = Scalr.resize(image, size);
		System.out.println("Write output...");
		ImageIO.write(out, OUTPUT_FORMAT, dest);
		System.out.println("Done !");
	}
}
